package us.sunrisemorning.douyuchat;

import java.io.IOException;

import org.tio.client.ClientGroupContext;
import org.tio.client.ReconnConf;
import org.tio.client.TioClient;
import org.tio.core.Node;

public class DouyuChatClient {
	private Node serverNode = new Node(Const.SERVER_NAME, Const.SERVER_PORT);

	private TioClient tioClient;

	public DouyuChatClient(String roomId, MessageHandler handler) throws IOException {
		ClientGroupContext clientGroupContext = new ClientGroupContext(
				new MessageClientAioHandler(new MessageListener(roomId, handler)),
				new MessageClientAioListener(roomId),
				new ReconnConf(Const.RECONNECT_INTERVAL));

		clientGroupContext.setHeartbeatTimeout(Const.HEARTBEAT_TIMEOUT);
		tioClient = new TioClient(clientGroupContext);
	}

	public void connect() throws Exception {
		tioClient.connect(serverNode);
	}
}
